<template>
	<view class="a" v-if="flag" @touchmove.stop.prevent="no()">
		<view class="renew">
			<view class="ground">
				<image src="../../../static/img/renew/new.png" mode="" style="width: 540rpx;height:684rpx"></image>
				<view class="btn1" @click="renew">马上更新</view>
			</view>
			<view class="icon-a-qingkongpng iconfont" style="color:#3C8EFF;font-size: 100rpx;" v-if="type==1"
				@click="cancel"> </view>

		</view>
	</view>
</template>

<script>
	export default {
		computed: {

		},
		created() {
			this.type = this.renewData.updateType
			console.log(this.renewData, '获取到的更新内容')
			// if(this.type==0){
			uni.hideTabBar()
			// }
		},
		props: {
			renewData: {
				type: Object,
				default: {}
			},
			flag: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				type: 1, //0强制更新 1不强制
				percent: 0,
			}
		},
		methods: {
			no() {
				return
			},
			renew() {
				console.log(this.renewData.address, '---------')
				this.downWgt(this.renewData.address)
			},

			cancel() {
				this.$emit('cancel')
				// this.bol=false
				uni.showTabBar()
			},
			async downWgt(address) {
				this.percent = 0
				let _this = this
				var wgtWaiting = plus.nativeUI.showWaiting("开始下载");
				var wgtUrl = address;
				var wgtOption = {
					filename: "_doc/update/",
					retry: 1
				};
				// 新建下载任务
				var task = await plus.downloader.createDownload(wgtUrl, wgtOption, function(download, status) {
					console.log(status, 'taskkkkkkkkkkkkkkkkkkkkkkk')
					if (status == 200) {
						wgtWaiting.close();
						_this.installWgt(task.filename)
						//安装wgt包文件
						// console.log(download,'downloaddownloaddownloaddownloaddownloaddownloaddownload')
						// _this.installWgt(download.filename);
					} else {
						_this.cancel()
						wgtWaiting.close();
						uni.showTabBar()
						// #ifdef APP-PLUS
						plus.nativeUI.toast('应用升级失败！')
						// #endif
						// uni.showToast({
						// 	title:'应用升级失败！',
						// 	icon:'none',
						// })
					}
				});

				task.addEventListener("statechanged", function(download, status) {
					console.log('已经下载多少', download, status)
					switch (download.state) {
						case 2:
							wgtWaiting.setTitle("已连接到服务器");
							// uni.showLoading({
							// 	title:'已连接到服务器'
							// })
							break;
						case 3:
							var percent = download.downloadedSize / download.totalSize * 100;
							_this.percent = percent > _this.percent ? percent : _this.percent
							wgtWaiting.setTitle("已下载 " + parseInt(_this.percent) + "%");
							break;
						case 4:
							wgtWaiting.setTitle("下载完成");
							break;
					}
				});
				task.start();
			},
			// 安装wgt文件
			installWgt(path) {
				console.log('安装文件111111111111', path);
				let _this = this
				console.log('开始安装')
				uni.showLoading({
					title: '正在安装'
				})
				plus.runtime.install(path, {
					force: true
				}, function() {
					uni.hideLoading()
					plus.nativeUI.toast('安装成功')
					uni.showTabBar()
					setTimeout(() => {
						plus.runtime.restart()
					}, 800)
					// uni.showToast({
					// 	icon:'none',
					// 	title:'安装成功！',
					// 	success() {
					// 		plus.runtime.restart()
					// 	}
					// })
				}, function(e) {
					uni.hideLoading()
					uni.showTabBar()
					console.log(e, 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee');
					uni.showModal({
						title: '安装失败',
						content: '请前往APP商城下载',
						confirmText: '确定',
						confirmColor: '#3C8EFF',
						success: function(res) {
							if (res.confirm) {
								uni.hideLoading()
								_this.cancel()
								console.log('用户点击确定');
							} else if (res.cancel) {
								uni.hideLoading()
								_this.cancel()
								console.log('用户点击取消');
							}
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.a {
		width: 100vw;
		height: 100vh;
		box-sizing: border-box;
		background: rgba(0, 0, 0, 0.5);
		padding-top: 300rpx
	}

	.ground {
		height: 700rpx;
		position: relative;
	}

	.renew {
		// background:#999999;
		width: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;

		image {
			display: block;

		}
	}

	.btn1 {
		width: 322rpx;
		height: 74rpx;
		position: absolute;
		background: #3C8EFF;
		color: #ffffff;
		font-weight: 500;
		font-size: 32rpx;
		border-radius: 38px;
		text-align: center;
		line-height: 74rpx;
		// background:rgba(0,0,0,0.5);
		bottom: 68rpx;
		left: 110rpx;
		border-radius: 56rpx;
	}
</style>